﻿/* ********************************************************************************
 * 名称：Cpn_RepositoryService
 * 描述：企业资料表  仓储服务接口实现类（本类通过CodeSmith软件生成生成）
 * 创建人：HFL
 * 创建日期：2020-06-01 17:48:55
 * 修改人：
 * 修改日期：
 * *******************************************************************************/
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Repository.Base;
using WPay.Infrastructure.Repository.DapperCore;
using WPay.Infrastructure.Unity;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using WPay.Domain;
using Model.DTOModel;
using System.Linq;
using System.Data.Entity;
using WPay.Domain.Model.Entities.DTOs.Req;
using WPay.Infrastructure.EFCore;
using Utility;
using System.IO;
using Model.PageModel;
using Utility.Enums;
using WPay.Domain.Model.DTOs.Parms;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api;
using Aop.Api.Util;
using System.Dynamic;
using WPay.Infrastructure.Unity.Enums;
using Model.DTO.Manager;
using WPay.Domain.Model;
using WPay.Domain.Model.DTOs.Lamada;
using Nest;
using NPOI.SS.Formula.Functions;

namespace WPay.Infrastructure.Repository.MainModule
{
    /// <summary>
    /// 企业资料表 仓储服务接口实现类
    /// </summary>
    public class Cpn_RepositoryService : RepositoryBase, ICpn_RepositoryService
    {
        /// <summary>
        /// 查询SQL语句
        /// </summary>
        private readonly string querySql = @"select CpnID,Name,CpnItTp,City,Adr,Tel,Fax,OpnBnk,ActNo,TaxID,Lnkr,LnkPhn,LnkEml,Cfo,CfoPhn,CfoEml,Cio,CioPhn,CioEml,Customer,OpnDt,Stt,Brf,Uptr,UptDtt,ERPCpnId,Deskey from Cpn where 1=1";
        /// <summary>
        /// 查询数据条数SQL语句
        /// </summary>
        private readonly string queryCountSql = @"select count(1) from Cpn where 1=1";
        /// <summary>
        /// 新增SQL语句
        /// </summary>
        private readonly string addSql = @"insert into Cpn(CpnID,Name,CpnItTp,City,Adr,Tel,Fax,OpnBnk,ActNo,TaxID,Lnkr,LnkPhn,LnkEml,Cfo,CfoPhn,CfoEml,Cio,CioPhn,CioEml,Customer,OpnDt,Stt,Brf,Uptr,UptDtt,ERPCpnId,Deskey) values (@CpnID,@Name,@CpnItTp,@City,@Adr,@Tel,@Fax,@OpnBnk,@ActNo,@TaxID,@Lnkr,@LnkPhn,@LnkEml,@Cfo,@CfoPhn,@CfoEml,@Cio,@CioPhn,@CioEml,@Customer,@OpnDt,@Stt,@Brf,@Uptr,@UptDtt,@ERPCpnId,@Deskey)";
        /// <summary>
        /// 修改所有字段SQL语句
        /// </summary>
        private readonly string editSql = @"update Cpn set Name=@Name,CpnItTp=@CpnItTp,City=@City,Adr=@Adr,Tel=@Tel,Fax=@Fax,OpnBnk=@OpnBnk,ActNo=@ActNo,TaxID=@TaxID,Lnkr=@Lnkr,LnkPhn=@LnkPhn,LnkEml=@LnkEml,Cfo=@Cfo,CfoPhn=@CfoPhn,CfoEml=@CfoEml,Cio=@Cio,CioPhn=@CioPhn,CioEml=@CioEml,Customer=@Customer,OpnDt=@OpnDt,Stt=@Stt,Brf=@Brf,Uptr=@Uptr,UptDtt=@UptDtt,ERPCpnId=@ERPCpnId,Deskey=@Deskey where 1=1";
        /// <summary>
        /// 表主键where条件SQL语句
        /// </summary>
        private readonly string whereByPrimaryKey = @" and CpnID=@CpnID";
        private readonly IEfCore_RepositoryService _efCore_RepositoryService;
        private Entities db;

        public Cpn_RepositoryService(IEfCore_RepositoryService efCore_RepositoryService)
        {
            _efCore_RepositoryService = efCore_RepositoryService;
            db = _efCore_RepositoryService.DbContext();
        }

        #region 由软件生成，禁止修改，若需要扩展，请在下方进行扩展

        /// <summary>
        /// 根据主键获取企业资料表资料信息
        /// </summary>
        /// <param name="_cpnID">企业代码</param>
        /// <returns>返回单个企业资料表数据实体</returns>
        public Cpn Get(string _cpnID)
        {
            string sql = querySql + whereByPrimaryKey;
            DynamicParameters param = new DynamicParameters();
            param.Add("CpnID", _cpnID);
            return base.QueryBySql<Cpn>(sql, param);
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_cpnID">企业代码</param>
        /// <returns>返回企业资料表数据集合</returns>
        public List<Cpn> GetAll(string _cpnID)
        {
            return base.QueryListBySql<Cpn>(querySql + " and CpnID=@CpnID", new { CpnID = _cpnID });
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <returns>返回企业资料表数据集合</returns>
        public List<Cpn> GetAll(Hashtable _param)
        {
            //查询SQL语句组装
            string sql = querySql + base.GetQueryConditions(_param);
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
            {
                dynamicParam.Add(key, _param[key]);
            }

            return base.QueryListBySql<Cpn>(sql, dynamicParam);
        }

        /// <summary>
        /// 按照分页条件查询数据集
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <param name="_pageIndex">查询页码，从1开始，必传</param>
        /// <param name="_pageSize">每页查询数据条数，必传</param>
        /// <param name="_sort">排序列名，若要降序请用列名+" DESC"，必传</param>
        /// <param name="_pageCount">返回总页码数</param>
        /// <param name="_dataCount">返回总数据条数</param>
        /// <returns>返回企业资料表数据集合</returns>
        public List<Cpn> GetByPage(Hashtable _param, int _pageIndex, int _pageSize, string _sort, out int _pageCount, out int _dataCount)
        {
            if (_pageIndex == 0 || _pageSize == 0 || string.IsNullOrWhiteSpace(_sort))
            {
                _dataCount = 0;
                _pageCount = 0;
                return null;
            }
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
                dynamicParam.Add(key, _param[key]);
            //组装计数sql语句
            string countSql = queryCountSql + base.GetQueryConditions(_param);
            _dataCount = base.QueryScalarBySql<Int32>(countSql, dynamicParam);//获取总数据条数
            _pageCount = (_dataCount + _pageSize - 1) / _pageSize;//总页数 =  (数据总条数 + 每页显示条数 - 1) / 每页显示条数
            if (_dataCount == 0)
            {
                return new List<Cpn>();
            }
            //组装分页查询语句
            string sql = querySql + base.GetQueryConditions(_param, _pageIndex, _pageSize, _sort);

            return base.QueryListBySql<Cpn>(sql, dynamicParam);
        }

        /// <summary>
        /// 插入企业资料表数据
        /// </summary>
        /// <param name="_cpn">需要插入的企业资料表数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(Cpn _cpn)
        {
            return base.ExecuteBySql(addSql, _cpn);
        }

        /// <summary>
        /// 批量插入企业资料表数据
        /// </summary>
        /// <param name="_cpnList">需要插入的企业资料表数据集合</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(List<Cpn> _cpnList)
        {
            return base.BulkInsert(_cpnList);
        }

        /// <summary>
        /// 根据主键更新企业资料表数据
        /// </summary>
        /// <param name="_cpn">需要更新的企业资料表数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(Cpn _cpn)
        {
            return base.ExecuteBySql(editSql + whereByPrimaryKey, _cpn);
        }

        /// <summary>
        /// 根据主键更新企业资料表指定字段
        /// </summary>
        /// <param name="_cpn">需要更新的企业资料表数据实体</param>
        /// <param name="_updateProNames">需要更新的字段名</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(Cpn _cpn, params string[] _updateProNames)
        {
            string sql = @"update Cpn set ";
            //主键值在修改时，禁止为空
            if (_cpn.CpnID == default(string))
                return 0;
            sql += base.GetUpdateSet(_updateProNames);
            return base.ExecuteBySql(sql + whereByPrimaryKey, _cpn);
        }

        #endregion

        /*业务扩展代码*/
        #region 业务扩展代码
      
        /// <summary>
        /// 获取企业下拉
        /// </summary>
        /// <returns></returns>
        public List<Cpn> GeCpntListItems(string cpnid)
        {

            if (string.IsNullOrEmpty(cpnid) || cpnid == "system" || cpnid == "undefined")
            {
                var query = (from a in db.Cpn select a).AsNoTracking();
                return query.ToList();
            }
            else
            {
                var query = (from a in db.Cpn.Where(a => a.CpnID == cpnid) select a).AsNoTracking();
                return query.ToList();
            }
        }
       
        /// <summary>
        /// 获取企业增长ID
        /// </summary>
        /// <returns></returns>
        public string GetMaxCpnId()
        {
            return base.QueryBySql<string>("select MAX(CpnID)+1 as id from Cpn where CpnID!='system'").ToString();
        }
       
       


       


     
        #endregion
    }
}
